<template>
  <div class="app-container">
    <div class="search-con">
      <div class="title">查询条件</div>
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="150px">
        <el-form-item label="商品Code" prop="itemCode">
          <el-input v-model="queryParams.itemCode" placeholder="请输入商品Code" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="商品名称" prop="title">
          <el-input v-model="queryParams.title" placeholder="请输入商品名称" clearable @keyup.enter="handleQuery" />
        </el-form-item>
      </el-form>
      <div class="search-btn-con">
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button type="info" icon="Refresh" @click="resetQuery">重置</el-button>
      </div>
    </div>
    <div class="main-con">
      <div class="title-con">
        <div class="title">基本信息</div>
        <div class="operate-btn-con">
          <el-button @click="handleRefresh" icon="Refresh" v-hasPermi="['invest:iitem:add']">刷新商品</el-button>
        </div>
      </div>
      <div class="content-con" v-loading="loading">
        <el-table v-loading="loading" :data="iitemList" @selection-change="handleSelectionChange">
          <!--       <el-table-column type="selection" width="55" align="center" />
          <el-table-column type="index" label="序号" :index="indexMethod" width="50"></el-table-column> -->
          <!-- <el-table-column label="商品id" align="center" prop="itemId" /> -->
          <el-table-column type="index" label="序号" :index="indexMethod" width="50"></el-table-column>
          <el-table-column label="商品Code" width="120" align="center" prop="itemCode" />
          <el-table-column label="商品名称" align="center" prop="title" />
          <el-table-column label="介绍" align="center" prop="content" />
          <el-table-column label="图片" align="center" prop="pictureV2">
            <template v-slot="scope">
              <el-image style="width: 100px" :src="scope.row.pictureV2" fit="fit"></el-image>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" width="160" prop="createTime" />
        </el-table>
        <el-pagination
          small
          background
          layout="total,sizes, prev, pager, next"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script setup name="Iitem">
import { listIitem, refreshItem } from '@/api/invest/iitem'
// eslint-disable-next-line no-unused-vars
import { require } from '@/utils/require'
const { proxy } = getCurrentInstance()

const iitemList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    itemCode: null,
    title: null,
    content: null
  }
})

const { queryParams } = toRefs(data)

/** 查询预约项目列表 */
function getList() {
  loading.value = true
  listIitem(queryParams.value).then((response) => {
    iitemList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}

// 分页
const handleCurrentChange = (val) => {
  queryParams.value.pageNum = val
  getList()
}

//每页显示条数改变
const handleSizeChange = (val) => {
  queryParams.value.pageSize = val
  getList()
}
// 序号翻页递增
const indexMethod = (index) => {
  const nowPage = queryParams.value.pageNum //当前第几页，根据组件取值即可
  const nowLimit = queryParams.value.pageSize //当前每页显示几条，根据组件取值即可
  return index + 1 + (nowPage - 1) * nowLimit // 这里可以理解成一个公式
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.itemId)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleRefresh() {
  refreshItem().then(() => {
    getList()
    proxy.$modal.msgSuccess('刷新成功')
  })
}

getList()
</script>
